;-------------------------------------------------------------------------------
format ELF64 executable
entry start

SDL_OPENGL = 0x04000000
SDL_NOFRAME = 0x02000000
SDL_KEYDOWN = 0x300

GL_FRAGMENT_SHADER = 0x8B30

WIN_W = 1280
WIN_H = 720

DEBUG = 0

macro emit [param] {param}
;-------------------------------------------------------------------------------
include 'import64.inc'
interpreter '/lib64/ld-linux-x86-64.so.2'
needed 'libdl.so.2'
import dlopen,dlsym
;-------------------------------------------------------------------------------
segment readable writeable executable

start:
    ;mov rbp,rsp
    sub rsp,2048
    ; define local variables
    emit <event equ rsp+1024>,<log equ rsp>
    mov r13,[dlsym]
if DEBUG = 1
    ; load libc
    emit <mov rdi,g_str_libc>,<xor esi,esi>,<inc esi>,<call [dlopen]>,<mov [g_libc],rax>
    emit <mov rdi,[g_libc]>,<mov rsi,g_str_printf>,<call r13>,<mov [printf],rax>
end if
    ; load SDL library
    emit <mov rdi,g_str_libsdl>,<xor esi,esi>,<inc esi>,<call [dlopen]>,<mov r12,rax>
    ; SDL_SetVideoMode
    emit <mov rdi,r12>,<mov rsi,g_str_SDL_SetVideoMode>,<call r13>
    emit <mov edi,WIN_W>,<mov esi,WIN_H>,<xor edx,edx>,<mov ecx,SDL_OPENGL+SDL_NOFRAME>,<call rax>
    ; load needed SDL functions
    emit <mov rdi,r12>,<mov rsi,g_str_SDL_GL_GetProcAddress>,<call r13>,<mov rbx,rax>
    ; create program
    emit <mov rdi,g_str_glCreateShaderProgramv>,<call rbx>
    emit <mov edi,GL_FRAGMENT_SHADER>,<mov esi,1>,<mov rdx,g_src_fs_ptr>,<call rax>,<mov r15d,eax>
    ; use program
    emit <mov rdi,g_str_glUseProgram>,<call rbx>
    emit <mov edi,r15d>,<call rax>
if DEBUG = 1
    ; print GLSL info log
    emit <mov rdi,g_str_glGetProgramInfoLog>,<call rbx>
    emit <mov edi,r15d>,<mov esi,1024>,<xor edx,edx>,<lea rcx,[log]>,<call rax>
    emit <mov rdi,g_str_log>,<lea rsi,[log]>,<call [printf]>
end if
      .main_loop:
    ; process window events
    emit <mov rdi,r12>,<mov rsi,g_str_SDL_PollEvent>,<call r13>
    emit <lea rdi,[event]>,<call rax>
    cmp dword [event],SDL_KEYDOWN
    je .main_loop_end
    ; get time
    emit <mov rdi,r12>,<mov rsi,g_str_SDL_GetTicks>,<call r13>,<call rax>,<mov r14d,eax>
    emit <mov rdi,g_str_glTexCoord1f>,<call rbx>
    emit <cvtsi2ss xmm0,r14d>,<call rax>
    ; draw
    emit <mov rdi,g_str_glRecti>,<call rbx>
    emit <mov edi,-1>,<mov esi,-1>,<mov edx,1>,<mov ecx,1>,<call rax>
    ; swap
    emit <mov rdi,r12>,<mov rsi,g_str_SDL_GL_SwapBuffers>,<call r13>,<call rax>
    jmp .main_loop
      .main_loop_end:
    ; terminate process
    emit <mov eax,60>,syscall
;-------------------------------------------------------------------------------
align 1
g_src_fs:
db  'float t;'
db  'float oa(vec3 q){'
db      'return cos(q.x)+cos(q.y*1.5)+cos(q.z)+cos(q.y*20.)*.05;'
db  '}'
db  'float ob(vec3 q){'
db      'return length(max(abs(q-vec3(cos(q.z*1.5)*.3,-.5+cos(q.z)*.2,0))-vec3(.125,.02,t+3.),vec3(0)));'
db  '}'
db  'float o(vec3 q){'
db      'return min(oa(q),ob(q));'
db  '}'
db  'vec3 gn(vec3 q){'
db      'vec3 f=vec3(.01,0,0);'
db      'return normalize(vec3(o(q+f.xyy),o(q+f.yxy),o(q+f.yyx)));'
db  '}'
db  'void main(){'
db      't=gl_TexCoord[0].s*.001;'
db      'vec2 p=-1.+2.*gl_FragCoord.xy/vec2(1280,720);'
db      'p.x*=1.6;'
db      'vec4 c=vec4(1);'
db      'vec3 org=vec3(sin(t)*.5,cos(t*.5)*.25+.25,t),dir=normalize(vec3(p.x*1.6,p.y,1)),q=org,pp;'
db      'float d=.0;'
db      'for(int i=0;i<64;i++){'
db          'd=o(q);'
db          'q+=d*dir;'
db      '}'
db      'pp=q;'
db      'float f=length(q-org)*.02;'
db      'dir=reflect(dir,gn(q));'
db      'q+=dir;'
db      'for(int i=0;i<64;i++){'
db          'd=o(q);'
db          'q+=d*dir;'
db      '}'
db      'c=max(dot(gn(q),vec3(.1,.1,0)),.0)+vec4(.3,cos(t*.5)*.5+.5,sin(t*.5)*.5+.5,1.)*min(length(q-org)*.04,1.);'
db      'if(oa(pp)>ob(pp))c=mix(c,vec4(cos(t*.3)*.5+.5,cos(t*.2)*.5+.5,sin(t*.3)*.5+.5,1.),.3);'
db      'gl_FragColor=((c+vec4(f))+(1.-min(pp.y+1.9,1.))*vec4(1,.8,.7,1))*min(t*.5,1.);'
db  '}',0

g_str_libsdl db 'libSDL-1.3.so.0',0
g_str_SDL_SetVideoMode db 'SDL_SetVideoMode',0
g_str_SDL_PollEvent db 'SDL_PollEvent',0
g_str_SDL_GL_SwapBuffers db 'SDL_GL_SwapBuffers',0
g_str_SDL_GetTicks db 'SDL_GetTicks',0
g_str_SDL_GL_GetProcAddress db 'SDL_GL_GetProcAddress',0
g_str_glCreateShaderProgramv db 'glCreateShaderProgramv',0
g_str_glRecti db 'glRecti',0
g_str_glUseProgram db 'glUseProgram',0
g_str_glTexCoord1f db 'glTexCoord1f',0
if DEBUG = 1
g_str_libc db 'libc.so.6',0
g_str_printf db 'printf',0
g_str_glGetProgramInfoLog db 'glGetProgramInfoLog',0
g_str_log db 'GLSL info log:',10,'%s',10,0
g_str_d db '%d',10,0
g_str_p db '%p',10,0
end if

align 8
g_src_fs_ptr dq g_src_fs

if DEBUG = 1
g_libc dq 0
printf dq 0
end if
;-------------------------------------------------------------------------------
